package com.xsq.consumer.dubboconsumer.filter;

import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.*;

/**
 * @ClassName TPMonitorFilter
 * @Description TODO
 * @Author xsq
 * @Date 2020/6/24 14:45
 **/
@Activate
public class TPMonitorFilter implements Filter {

    private TPMonitor tpMonitor = new TPMonitor();

    /**
     * 初始化时默认会加载
     */
    public TPMonitorFilter() {
        new Thread(tpMonitor).start();
    }


    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        Long startTime = System.currentTimeMillis();
        Result result = invoker.invoke(invocation);
        Long time = System.currentTimeMillis() - startTime;
        tpMonitor.record(System.currentTimeMillis(), time);
        return result;
    }
}
